var gulp = require('gulp');
var gutil = require('gulp-util');
var bower = require('bower');
var concat = require('gulp-concat');
var sass = require('gulp-sass');
var sourcemaps = require('gulp-sourcemaps');
var minifyCss = require('gulp-minify-css');
var rename = require('gulp-rename');
var sh = require('shelljs');
var templateCache = require('gulp-angular-templatecache');
var ngAnnotate = require('gulp-ng-annotate');  //压缩的关键
var uglify = require('gulp-uglify');
var del = require('del');
var stripDebug = require('gulp-strip-debug');
//var sprite = require('css-sprite').stream;

var paths = {
    sass: ['./scss/**/*.scss'],
    appsass: ['./scss/ionic.app.scss'],
    templates: ['./www/views/**/*.html'],
    scripts: ['./www/js/*.js'],
    apis: ['./www/js/apis/**/*.js'],
    controllers: ['./www/js/controllers/**/*.js'],
    services: ['./www/js/services/**/*.js'],
    directives: ['./www/js/directives/*.js'],
    configs: ['./www/js/configs/*.js']
};

/***************************
 *                         *
 *       Outer Tasks       *
 *                         *
 ***************************/

// default task
gulp.task('default', ['sass','template-concat','api-concat',
    'controller-concat','service-concat','directive-concat', 'config-concat']);

// build whole project
gulp.task('build', ['clean','default'], function() {
    gulp.start('concat-js','concat-css');
});

// watch assets
gulp.task('watch', function() {
    gulp.watch(paths.sass, ['sass']);
    gulp.watch(paths.templates, ['template-concat']);
    //gulp.watch(paths.controllers, ['controller-concat']);
    //gulp.watch(paths.services, ['service-concat']);
    //gulp.watch(paths.directives, ['directive-concat']);
});

/***************************
 *                         *
 *       Inner Tasks       *
 *                         *
 ***************************/

// clean dist
gulp.task('clean', function(cb) {
    del(['./dist/'], cb)
});

// compile sass
gulp.task('sass', function(done) {
    gulp.src(paths.appsass[0])
        .pipe(sourcemaps.init())
        .pipe(sass({
            errLogToConsole: true
        }))
        .pipe(gulp.dest('./www/css/'))
        .pipe(minifyCss({
            keepSpecialComments: 0
        }))
        .pipe(rename({ extname: '.min.css' }))
        .pipe(sourcemaps.write('./maps'))
        .pipe(gulp.dest('./www/css/'))
        .on('end', done);
});

// template concat
gulp.task('template-concat', function (done) {
    gulp.src(paths.templates[0])
        .pipe(templateCache({
            standalone:true,
            module: 'my-tabs.templates',
            base: function(file) {
                return 'views/'+ file.relative;
            }
        }))
        .pipe(gulp.dest('./www/js'))
        .on('end', done);
});

// controller concat
gulp.task('controller-concat',function(done){
    gulp.src(paths.controllers[0])
        .pipe(concat('controllers.js'))
        .pipe(gulp.dest('./www/js'))
        .on('end', done);
});

// service concat
gulp.task('service-concat',function(done){
    gulp.src(paths.services[0])
        .pipe(concat('services.js'))
        .pipe(gulp.dest('./www/js'))
        .on('end', done);
});

// directive concat
gulp.task('directive-concat',function(done){
    gulp.src(paths.directives[0])
        .pipe(concat('directives.js'))
        .pipe(gulp.dest('./www/js'))
        .on('end', done);
});

// api concat
gulp.task('api-concat',function(done){
    gulp.src(paths.apis[0])
        .pipe(concat('apis.js'))
        .pipe(gulp.dest('./www/js'))
        .on('end', done);
});

// config concat
gulp.task('config-concat',function(done){
    gulp.src(paths.configs[0])
        .pipe(concat('configs.js'))
        .pipe(gulp.dest('./www/js'))
        .on('end', done);
});

// script concat
gulp.task('concat-js',function(done) {
    gulp.src(paths.scripts[0])
        .pipe(stripDebug())
        .pipe(concat('script.js'))
        .pipe(gulp.dest('./www/dist/js'))
        .pipe(rename({suffix: '.min'}))
        .pipe(ngAnnotate())
        .pipe(uglify()) //压缩
        .pipe(gulp.dest('./www/dist/js'))
        .on('end', done);
});

// css concat
gulp.task('concat-css',function(done){
    gulp.src(paths.appsass[0])
        .pipe(sourcemaps.init())
        .pipe(sass({
            errLogToConsole: true
        }))
        .pipe(gulp.dest('./www/dist/css/'))
        .pipe(minifyCss({
            keepSpecialComments: 0
        }))
        .pipe(rename({ extname: '.min.css' }))
        .pipe(sourcemaps.write('./maps'))
        .pipe(gulp.dest('./www/dist/css/'))
        .on('end', done);
});

// bower install libs
gulp.task('install', ['git-check'], function() {
    return bower.commands.install()
        .on('log', function(data) {
            gutil.log('bower', gutil.colors.cyan(data.id), data.message);
        });
});

// check git
gulp.task('git-check', function(done) {
    if (!sh.which('git')) {
        console.log(
            '  ' + gutil.colors.red('Git is not installed.'),
            '\n  Git, the version control system, is required to download Ionic.',
            '\n  Download git here:', gutil.colors.cyan('http://git-scm.com/downloads') + '.',
            '\n  Once git is installed, run \'' + gutil.colors.cyan('gulp install') + '\' again.'
        );
        process.exit(1);
    }
    done();
});
